<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { PureTableBar } from "@/components/RePureTableBar";
import { hasAuth } from "@/router/utils";
import SearchIcon from "@iconify-icons/ri/search-line";
import { ref } from "vue";
import { useUser } from "./utils/hook";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "ResearchAchiev"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  tableCol,
  loading,
  dataList,
  pagination,
  columns,
  onSearch,
  resetForm,
  openDialog,
  selectOpt,
  allUserList,
  doChooseAchiev,
  showDetail,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  exportModuleData
} = useUser(tableRef);
</script>

<template>
  <div style="box-shadow: none">
    <div class="w-full">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="所属团队" prop="researchTeamId">
          <el-select
            v-model="form.researchTeamId"
            placeholder="请选择"
            clearable
            class="!w-[200px]"
          >
            <el-option
              v-for="item in selectOpt.tdList"
              :key="item.id"
              :label="item.teamName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="masterUserId">
          <el-select
            v-model="form.masterUserId"
            clearable
            filterable
            placeholder="请选择"
            class="!w-[180px]"
          >
            <el-option
              v-for="item in allUserList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="审核状态" prop="examineStatus">
          <el-select
            v-model="form.examineStatus"
            placeholder="请选择"
            clearable
            class="!w-[180px]"
          >
            <el-option label="审核中" :value="0" />
            <el-option label="已审核" :value="1" />
            <el-option label="已驳回" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择"
            clearable
            class="!w-[180px]"
          >
            <el-option label="进行中" :value="1" />
            <el-option label="已完成" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="关键词" prop="keywords">
          <el-input
            v-model="form.keywords"
            placeholder="请输入"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item label="所属学科" prop="subject">
          <el-input
            v-model="form.subject"
            placeholder="请输入"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item label="所属领域" prop="domain">
          <el-input
            v-model="form.domain"
            placeholder="请输入"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <PureTableBar
        v-if="tableCol.load"
        title=""
        :tableCol="tableCol"
        :columns="columns"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            v-if="hasAuth('researchAchiev:btn:add')"
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog('新增')"
          >
            新增
          </el-button>
          <el-button
            v-if="hasAuth('researchAchiev:btn:export')"
            type="success"
            @click="exportModuleData"
          >
            导出
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 90 }"
            align-whole="center"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(SearchIcon)"
                @click="showDetail(row)"
              >
                查看
              </el-button>
              <el-button
                v-if="hasAuth('researchAchiev:btn:up')"
                class="reset-margin !ml-[5px]"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('修改', row)"
              >
                修改
              </el-button>
              <el-button
                v-if="hasAuth('researchAchiev:btn:addAchiev')"
                class="reset-margin !ml-[5px]"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="doChooseAchiev(row)"
              >
                添加成果
              </el-button>
              <template
                v-if="
                  row.examineStatus != 1 && hasAuth('researchAchiev:btn:del')
                "
              >
                <el-popconfirm
                  title="是否确认删除?"
                  @confirm="handleDelete(row)"
                >
                  <template #reference>
                    <el-button
                      class="reset-margin !ml-[5px]"
                      link
                      type="danger"
                      :size="size"
                      :icon="useRenderIcon(Delete)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </template>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
.teams {
  min-height: 250px;
  display: flex;
  flex-wrap: wrap;
  .team {
    width: 330px;
    padding: 12px 0 12px 12px;
    margin: 0 15px 15px 0;
    border-radius: 5px;
    background-color: #fff;
    cursor: pointer;
    .cell {
      font-size: 13px;
      display: flex;
      align-items: center;
      margin-top: 10px;
      font-weight: bold;
      .c-title {
        color: #666;
        min-width: 65px;
      }
      .c-content {
        color: black;
        overflow: hidden;
        word-wrap: break-word;
        line-height: 20px;
      }
    }
    .spec {
      display: flex;
      flex-wrap: wrap;
      margin-top: 6px;
    }
    .price-box {
      color: red;
      font-size: 14px;
      .price {
        font-size: 20px;
        margin-left: 2px;
      }
      .zk-price {
        color: #999;
        margin-left: 10px;
        text-decoration: line-through;
      }
    }
    .opt {
      margin-top: 12px;
      text-align: right;
      padding-right: 12px;
      display: flex;
      justify-content: flex-end;
      .btn {
        width: 100%;
        height: 30px;
        line-height: 30px;
        display: flex;
        .edit {
          font-size: 12px;
          width: 60px;
          height: 100%;
          color: #fff;
          background-color: #00a6f3;
          border-radius: 4px;
          margin-right: 10px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
        .del {
          width: 35px;
          text-align: center;
          border: 1px solid red;
          border-radius: 4px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
      }
    }
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  padding-right: 15px;
  margin-top: 15px;
}
</style>
